var app = getApp();
var com = require('../../common.js');
var touchDotX = 0; //X按下时坐标
var touchDotY = 0; //y按下时坐标
var interval; //计时器
var time = 0; //从按下到松开共多少时间*100
Page({
  data: {
    list: [{
      username: '321321',
      phone_number: '3213',
    }],
    onPageDelete: false
  },
  onLoad: function(options) {},
  onReady: function() {},
  onShow: function() {
    var _this = this
    _this.onShopList()
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {},
  onShopList() {
    var _this = this
    com.link(app.link.gropshop, {}, function(res) {
      console.log(res);
      res.data.data.map((v, k) => {
        res.data.data[k].click = false
      })
      _this.setData({
        list: res.data.data
      })
    }, "GET")
  },
  open(e) {
    var _this = this
    wx.navigateTo({
      url: '/pages/shopGropInfo/shopGropInfo?id=' + e.currentTarget.id,
    })
  },
  // 触摸开始事件
  touchStart: function(e) {
    touchDotX = e.touches[0].pageX; // 获取触摸时的原点
    touchDotY = e.touches[0].pageY;
    // 使用js计时器记录时间    
    interval = setInterval(function() {
      time++;
    }, 100);
  },
  touchmove() {
    var _this = this
    _this.setData({
      onPageDelete: true
    })
  },
  // 触摸结束事件
  touchEnd: function(e) {
    var onPageDelete = this.data.onPageDelete
    if (onPageDelete) {
      let touchMoveX = e.changedTouches[0].pageX;
      let touchMoveY = e.changedTouches[0].pageY;
      let tmX = touchMoveX - touchDotX;
      let tmY = touchMoveY - touchDotY;
      var _this = this,
        list = _this.data.list
      if (time < 20) {
        let absX = Math.abs(tmX);
        let absY = Math.abs(tmY);
        if (absX > 2 * absY) {
          if (tmX < 0) {
            list.map((v, k) => {
              if (list[k].id == e.currentTarget.id) {
                list[k].click = true
              }else{
                list[k].click = false
              }
            })
            _this.setData({
              list: list,
              onPageDelete: false
            })
            console.log("左滑")
          } else {
            list.map((v, k) => {
              if (list[k].id == e.currentTarget.id) {
                list[k].click = false
              }
            })
            _this.setData({
              list: list,
              onPageDelete: false
            })
            console.log("右滑")
          }
        }
      }
      clearInterval(interval); // 清除setInterval
      time = 0;
    } else {
      clearInterval(interval); // 清除setInterval
      time = 0;
    }
  },
  shopInfo: function(e) {
    wx.navigateTo({
      url: '/pages/shopInfo/shopInfo?id=' + e.currentTarget.dataset.id + '&cid=' + e.currentTarget.id +'&name=' +e.currentTarget.dataset.name, 
    })
  },
  //删除门店
  onDelete(e) {
    var _this = this
    wx.showModal({
      title: '温馨提示',
      content: '确认删除该门店吗？',
      success(res) {
        if (res.confirm) {
          com.link(app.link.onGropDelete + e.currentTarget.id + '.st', {}, function(res) {
            console.log(res)
            if (res.data.state) {
              wx.showToast({
                title: res.data.message,
              })
              setTimeout(() => {
                _this.onShopList()
              }, 1000)
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          }, "GET")
        }
      }
    })

  },
})